package com.hand.cloud.b2b.taobao;

import de.hybris.platform.core.model.order.OrderModel;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hand.cloud.b2b.daos.EPOrderDAO;
import com.hand.cloud.b2b.daos.OrderDAO;
import com.hand.cloud.b2b.daos.TBTradeDAO;
import com.hand.cloud.b2b.model.EPOrderModel;
import com.hand.cloud.b2b.model.TB_TradeModel;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Trade;
import com.taobao.api.request.TradeFullinfoGetRequest;
import com.taobao.api.response.TradeFullinfoGetResponse;


public class TradeFullinfo
{
	public static Logger LOG = Logger.getLogger(TradeFullinfo.class);

	@Autowired
	private TBTradeDAO tbTradeDAO;

	@Autowired
	private EPOrderDAO epOrderDAO;

	@Autowired
	private OrderDAO orderDAO;


	public void execute(final TaobaoClient client, final String sessionKey, final long tid)
	{

		final TradeFullinfoGetRequest req = new TradeFullinfoGetRequest();
		req.setFields("seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee,adjust_fee,post_fee,total_fee,pay_time,end_time,modified,consign_time,buyer_obtain_point_fee,point_fee,real_point_fee,received_payment,commission_fee,pic_path,num_iid,num_iid,num,price,cod_fee,cod_status,shipping_type,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders.title,orders.pic_path,orders.price,orders.num,orders.iid,orders.num_iid,orders.sku_id,orders.refund_status,orders.status,orders.oid,orders.total_fee,orders.payment,orders.discount_fee,orders.adjust_fee,orders.sku_properties_name,orders.item_meal_name,orders.buyer_rate,orders.seller_rate,orders.outer_iid,orders.outer_sku_id,orders.refund_id,orders.seller_type");
		req.setTid(tid);

		try
		{
			final TradeFullinfoGetResponse response = client.execute(req, sessionKey);
			System.out.println("getTrade:" + response.getBody());
			final Trade trade = response.getTrade();

			System.out.println("trade.getBuyerAlipayNo(): " + trade.getBuyerAlipayNo());
			System.out.println("trade.getDiscountFee(): " + trade.getDiscountFee());
			System.out.println("trade.getHasPostFee(): " + trade.getHasPostFee());
			System.out.println("trade.getInvoiceName(): " + trade.getInvoiceName());
			System.out.println("trade.getInvoiceType(): " + trade.getInvoiceType());
			System.out.println("trade.getPayment(): " + trade.getPayment());
			System.out.println("trade.getPointFee(): " + trade.getPointFee());
			System.out.println("trade.getPostFee(): " + trade.getPostFee());
			System.out.println("trade.getSellerCodFee(): " + trade.getSellerCodFee());
			System.out.println("trade.getSnapshotUrl(): " + trade.getSnapshotUrl());
			System.out.println("trade.getStatus(): " + trade.getStatus());
			System.out.println("trade.getTotalFee(): " + trade.getTotalFee());
			System.out.println("trade.getType(): " + trade.getType());


			// 创建tbOrder
			final TB_TradeModel tbTrade = this.tbTradeDAO.createTBTrade(trade);
			System.out.println("tbTradePK:" + tbTrade.getPk());

			final EPOrderModel epOrder = this.epOrderDAO.createEpOrder(tbTrade);
			System.out.println("epOrderPK:" + epOrder.getPk());

			final OrderModel order = this.orderDAO.createOrder(epOrder);
			System.out.println("orderPK:" + order.getPk());

			System.out.println("地址:" + trade.getReceiverAddress());
		}
		catch (final ApiException e)
		{
			e.printStackTrace();
		}

	}
}
